
var WebSocket = WebSocket || window.WebSocket || window.MozWebSocket;

//网络列队处理
function NetQueueProcess() {
    g_NetQuene.process();
};

//调用类型
var NET_QUENE_UNKONW = 0;
var NET_QUENE_CONNECT = 1;
var NET_QUENE_OPEN = 2;
var NET_QUENE_MESSAGE = 3;
var NET_QUENE_CLOSE = 4;
var NET_QUENE_ERROR = 5;
var NET_QUENE_DISCONNECT = 6;
var NET_QUENE_SEND = 7;

//网络类型
var NetQueueType = cc.Class({
    ctor :function () {
        this.m_Type = arguments[0];
        this.m_Prama1 = arguments[1];
        this.m_Prama2 = arguments[2];
        this.m_Prama3 = arguments[3];

    },
});

//网络列队
var NetQueue = cc.Class({
    ctor :function () {
        this.m_Queue = new Array();
    },

    push :function (Type, Prama1, Prama2, Prama3) {
        this.m_Queue.push(new NetQueueType(Type, Prama1, Prama2, Prama3));
    },

    process :function () {
        if (this.m_Queue.length <= 0)return;
        if (window.g_Socket==null)return;
        switch (this.m_Queue[0].m_Type) {
            case NET_QUENE_CONNECT:
                window.g_Socket.mSocket = new WebSocket("ws://"+this.m_Queue[0].m_Prama2+":"+this.m_Queue[0].m_Prama3);
                window.g_Socket.mSocket.binaryType = "arraybuffer";
                window.g_Socket.mSocket.onopen = this.m_Queue[0].m_Prama1.onopen;
                window.g_Socket.mSocket.onmessage = this.m_Queue[0].m_Prama1.onmessage;
                window.g_Socket.mSocket.onclose = this.m_Queue[0].m_Prama1.onclose;
                window.g_Socket.mSocket.onerror = this.m_Queue[0].m_Prama1.onerror;
                break;
            case NET_QUENE_OPEN:
                window.g_Socket.mSocketSink.onSocketLink(this.m_Queue[0].m_Prama2);
                break;
            case NET_QUENE_MESSAGE:
                window.g_Socket.mSocketSink.onSocketData(this.m_Queue[0].m_Prama2.data);
                break;
            case NET_QUENE_CLOSE:
                if (window.g_Socket.mSocketSink) window.g_Socket.mSocketSink.onSocketShut();
                break;
            case NET_QUENE_ERROR:
                if(LOG_NET_DATA)console.log("WebScoket Error:");
                if(LOG_NET_DATA)console.log(this.m_Queue[0].m_Prama2);
                if (window.g_Socket.mSocketSink)window.g_Socket.mSocketSink.onSocketError(0);
                break;
            case NET_QUENE_DISCONNECT:
                if(LOG_NET_DATA)console.log("NET_QUENE_DISCONNECT 主动关闭");
                window.g_Socket.mSocket.close();
                window.g_Socket.mSocket.readyState = 0;
                window.g_Socket.mSocket = null;
                break;
            case NET_QUENE_SEND:
                if (window.g_Socket.mSocket == null)break;
                window.g_Socket.mSocket.send(this.m_Queue[0].m_Prama2);
                break;
            default:
                if(LOG_NET_DATA)console.log("无效WebScoket包!");
        }
        this.m_Queue.shift();
    }
});

var g_NetQuene = new NetQueue();

cc.CSocket = cc.Class({

    ctor :function () {
        this.mSocketSink = arguments[0];
        this.mSocket = null;
    },

    isAlive :function (){
        if(window.g_Socket.mSocket==null)return false;
        return window.g_Socket.mSocket.readyState == 1;
    },

    connect :function(url,port){
        setInterval(NetQueueProcess, 1);//1000为1秒钟
        g_NetQuene.push(NET_QUENE_CONNECT,this,url,port);
    },

    onopen :function(event){
        if(LOG_NET_DATA)console.log("网络连接成功！")
        window.g_Socket.mSocket.mSocketID = window.g_SocketID;
        g_NetQuene.push(NET_QUENE_OPEN, this, event);
    },

    // 监听消息
    onmessage :function(event){
       if(event.target.mSocketID != window.g_SocketID)return;
       g_NetQuene.push(NET_QUENE_MESSAGE, this, event);
    },

    // 监听Socket的关闭
    onclose :function(event){
        if(window.g_Socket==null)return;
        if(!window.g_Socket.isAlive())return;
        if(event.target.mSocketID != window.g_SocketID)return;
        if(LOG_NET_DATA)console.log("onclose 主动关闭");
        window.g_Socket.mSocket.close();
        window.g_Socket.mSocket = null;
        g_NetQuene.push(NET_QUENE_CLOSE, this, event);
    },

    onerror :function(event){
        g_NetQuene.push(NET_QUENE_ERROR, this, event);
    },

    disconnect :function(){
        if(LOG_NET_DATA)console.log('disconnect 关闭网络!');
        if(window.g_Socket.mSocket!=null)window.g_Socket.mSocket.close();
        window.g_Socket.mSocket = null;
        delete window.g_Socket;
    },

    send :function(data){
        g_NetQuene.push(NET_QUENE_SEND, this, data);
    },
});

//g_Socket = new cc.CSocket;